package com.quanyan.ic.datacenter.http.service.impl;

import com.quanyan.ic.datacenter.dao.*;
import com.quanyan.ic.datacenter.http.req.ReqEventAlarms;
import com.quanyan.ic.datacenter.http.req.ReqHandleAlarm;
import com.quanyan.ic.datacenter.http.resp.RespCategoryAlarms;
import com.quanyan.ic.datacenter.http.resp.RespEventAlarms;
import com.quanyan.ic.datacenter.http.service.AlarmsHandleService;
import com.quanyan.ic.datacenter.mapper.icData.EventAlarmMapper;
import com.quanyan.ic.datacenter.mapper.icData.PlaceUnitHandelTypeMapper;
import com.quanyan.ic.datacenter.mapper.icData.entity.PlaceUnitHandelType;
import com.quanyan.ic.datacenter.mapper.icData.entity.PlaceUnitHandelTypeExample;
import com.quanyan.ic.datacenter.model.PlaceUnitInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by jian.zhou on 2017/8/24.
 */
@Service
public class AlarmsHandleServiceImpl implements AlarmsHandleService {

    @Autowired
    private EventAlarmMapper eventAlarmMapper;

    @Autowired
    private EventAlarmDao eventAlarmDao;

    @Autowired
    private PlaceUnitVideoAlarmHandleDao placeUnitVideoAlarmHandleDao;

    @Autowired
    private PlaceDao placeDao;

    @Autowired
    private AlarmsHandelReportCityDao alarmsHandelReportCityDao;

    @Autowired
    private AlarmsHandelReportPlaceDao alarmsHandelReportPlaceDao;

    @Autowired
    private PlaceUnitHandelTypeMapper placeUnitHandelTypeMapper;

    @Value("${hik8710.server.img.baseUrl}")
    private String prefix;

    @Override
    public List<RespEventAlarms> findEventAlarms(ReqEventAlarms reqEventAlarms) {
        List<RespEventAlarms> list = eventAlarmMapper.findEventAlarms(reqEventAlarms);
        if ( list != null && list.size() > 0){
            for (RespEventAlarms respEventAlarms : list ){
               String str =  respEventAlarms.getScreenshotUrls();
                if (str != null){
                   String [] strArray = str.split(",");
                    for (String s:strArray){
                        if (s.length()>5){
                            String img =  prefix +"/" +   s.substring(3,str.length()-2);
                            respEventAlarms.getScreenshotUrl().add(img);
                        }

                    }
                }
            }
        }

        return list;
    }

    @Override
    public List<RespCategoryAlarms> findPlaceCategory(ReqEventAlarms reqEventAlarms) {
        return eventAlarmDao.findPlaceCategory(reqEventAlarms);
    }

    @Override
    @Transactional
    public void handleAlarm(ReqHandleAlarm reqHandleAlarm){
        placeUnitVideoAlarmHandleDao.handel(reqHandleAlarm);
        PlaceUnitInfo placeUnitInfo = placeDao.getPlaceUnitInfo(reqHandleAlarm.getSiteId());
        alarmsHandelReportCityDao.insert(placeUnitInfo,reqHandleAlarm);
        alarmsHandelReportPlaceDao.insert(placeUnitInfo,reqHandleAlarm);

    }

    @Override
    public int isHaveAlarm(Integer placeId) {
        return placeUnitVideoAlarmHandleDao.isHaveAlarm(placeId);
    }

    @Override
    public List<PlaceUnitHandelType> getHandleTypes(byte category) {
        PlaceUnitHandelTypeExample example = new PlaceUnitHandelTypeExample();
        example.createCriteria()
                .andCategoryEqualTo(category)
                .andIsEnableEqualTo(true);
        return placeUnitHandelTypeMapper.selectByExample(example);
    }

}